layui.define(['element', 'api', 'amis', 'schema', 'schema_render', 'listener', 'ztree', 'utils'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'resource_content',
		$ = layui.jquery,
		api = layui.api,
        amis = layui.amis,
        schema = layui.schema,
        listeners = layui.listener,
        schema_render = layui.schema_render,
        ztree = layui.ztree,
        utils = layui.utils,
        contentElem,
        viewMode = 'tabs',
        schemaType=null
    ;

    /**
     * 资源内容_单击树监听器
     * @param {} event 
     * @param {*} treeId 
     * @param {*} treeNode 
     */
     function resource_content_resourceTreeClickListener(event, treeId, treeNode, option, pearBody, resource_tabs_obj){
        var treeObj = $.fn.zTree.getZTreeObj(treeId)
        var schemaType = treeObj.setting.view.schemaType;

        var metaType = treeNode.data.type.metaType;
        var resourceTypeSchema = schema.get(schemaType);
        var typeSchema = resourceTypeSchema[metaType];
        if(typeSchema.contentPanel){
            sessionStorage.setItem('content-schemaType',JSON.stringify(schemaType))
            sessionStorage.setItem('currentTreeId',treeId)

            setTimeout(function(){
                layui.resource_content.render({
                    elem: treeNode.tId,
                    mode: 'schema',
                    treeNode: treeNode,
                    viewMode:viewMode,
                    schemaType:schemaType,
                    amisData: utils.getGlobalEnv(treeNode.data),
                    resourceTreeOption: option,
                    pearBody : pearBody,
                    resource_tabs : resource_tabs_obj
                })
            },100)
            // layui.resource_content.render({
            //     elem: treeNode.tId,
            //     mode: 'schema',
            //     treeNode: treeNode,
            //     viewMode:viewMode,
            //     schemaType:schemaType,
            //     amisData: utils.getGlobalEnv(treeNode.data),
            //     resourceTreeOption: option,
            //     pearBody : pearBody,
            //     resource_tabs : resource_tabs_obj
            // })
        }
    }
    
    /**
     * 模块装配
     */
    function configur(){
        listeners.registerResourceTreeClickListener('resource_content_resourceTreeClickListener', resource_content_resourceTreeClickListener);
    }
    
    

	var resource_content = function(opt) {
		this.option = opt;
	};


    function resetContent(contentElem){
        $(contentElem).remove();
        $('.layui-body.gs-body').append('<div id="'+contentElem.slice(1)+'"></div>')
    }




    /**
	 * Button start loading
	 * */
    resource_content.prototype.render = function(opt) {
		
		var option = {
			elem: opt.elem,
            treeNode: opt.treeNode,
            amisData: opt.amisData,
            schemaType:opt.schemaType,
            mode: opt.mode,
            function: opt.function,

            pearBody: opt.pearBody,
            resourceTreeOption: opt.resourceTreeOption,
            resource_tabs: opt.resource_tabs
		}

        contentElem = option.elem;

        var contentFunction, contentFunctionParamsData;
        if(option.mode === 'schema'){
            contentFunction = schemaRender;
            contentFunctionParamsData = {option : option, metaType: option.treeNode.data.type.metaType};
        }else if(option.mode === 'function' && typeof option.function === 'function'){
            contentFunction = option.function
            contentFunctionParamsData = contentElem;
        }

        //初始化tab标签
        opt.resource_tabs.addTab({
            id: contentElem,
            title: option.treeNode.title,
            data : option.treeNode,
            contentFn: {
                fn: contentFunction,
                data: contentFunctionParamsData
            }
        })

        return new resource_content(option);
	}


    function schemaRender(opt){
        var {option, metaType} = opt;
        var resourceTypeSchema = schema.get(option.schemaType);
        var typeSchema = resourceTypeSchema[metaType];

        if(typeSchema.contentPanel){

            var id = typeSchema.id;
            var contentPanel = typeSchema.contentPanel;

            if(contentPanel.mode === 'listener'){
                var listenerName;
                if(contentPanel.listenerName){
                    listenerName = contentPanel.listenerName;
                }else{
                    listenerName = id + '_contentPanel_listener';
                }
                var listener = listeners.getContentPanelListener(listenerName);
                if(listener &&  typeof listener === 'function'){
                    listener(
                        option.treeNode,
                        {
                            id: option.elem.replace('#', ''),
                            title: ''
                        },
                        option.pearBody,
                        option.resourceTreeOption,
                        option.resource_tabs
                    )
                }
            }
            else{
                schema_render.render({
                    elem: utils.getTabElem(option.treeNode.tId),
                    schema: contentPanel,
                    contextData: option.treeNode
                })
            }

        }
    }

    configur();
    exports(MOD_NAME, new resource_content());
});
